package com.zhouyibike.uu.ui.activity.yihoudong;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.ui.adapter.MytablayoutvpAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.Util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MyYiquanmingxiActivity extends BaseActivity {
    private TabLayout tabs;
    private ViewPager viewPager;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();
    private MytablayoutvpAdapter adpater;
    private MyYiQuanFragment f1,f2,f3,f4,f5,f6,f7,f8;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_yiquanmingxi);
        initView();
    }
    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("翼券明细");
        viewPager= (ViewPager) findViewById(R.id.myyiquanvp);
        tabs= (TabLayout) findViewById(R.id.myyiquantblayout);
        mTitle.add("全部");
        mTitle.add("购买翼券");
        mTitle.add("转让翼券");
        mTitle.add("推荐奖励");
        f1=new MyYiQuanFragment();
        f2=new MyYiQuanFragment();
        f3=new MyYiQuanFragment();
        f4=new MyYiQuanFragment();
        mFragment.add(f1);
        mFragment.add(f2);
        mFragment.add(f3);
        mFragment.add(f4);
        adpater= new MytablayoutvpAdapter(getSupportFragmentManager(), mTitle, mFragment);
        viewPager.setAdapter(adpater);
        //为TabLayout设置ViewPager
        tabs.setupWithViewPager(viewPager);
        //使用ViewPager的适配器
        tabs.setTabsFromPagerAdapter(adpater);
        tabs.setTabTextColors(getResources().getColor(R.color.black80),getResources().getColor(R.color.color_normal));//字体颜色
        tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_normal));

        for (int i = 0; i <mTitle.size() ; i++) {
            Bundle bundle = new Bundle();
            bundle.putString("type",(1+i)+"");
            mFragment.get(i).setArguments(bundle);
        }
        reflex(tabs);
    }
    public void reflex(final TabLayout tabLayout){
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = Util. dp2px(tabLayout.getContext(), 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width ;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
